<?php
session_start();

// 检查是否已安装
if (!file_exists(__DIR__ . '/config/config.php')) {
    header('Location: install.php');
    exit;
}

require_once __DIR__ . '/config/config.php';

$error = '';


// 检查是登录还是注册请求
$is_login = isset($_GET['action']) && $_GET['action'] === 'login';
$is_register = isset($_GET['action']) && $_GET['action'] === 'register';
$is_register = isset($_GET['action']) && $_GET['action'] === 'admin';
// 处理登录表单提交
if ($_POST && isset($_POST['login'])) {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (empty($username) || empty($password)) {
        $error = '用户名和密码都是必填的';
    } else {
        // 检查用户文件夹是否存在
        $user_dir = __DIR__ . '/users/' . $username;
        $profile_file = $user_dir . '/profile.json';
        
        if (file_exists($profile_file)) {
            $profile = json_decode(file_get_contents($profile_file), true);
            if ($profile && password_verify($password, $profile['password'])) {
                $_SESSION['user_logged_in'] = true;
                $_SESSION['username'] = $profile['username'];
                $_SESSION['user_nickname'] = $profile['nickname'];
                header('Location: index.php');
                exit;
            } else {
                $error = '用户名或密码错误';
            }
        } else {
            $error = '用户不存在';
        }
    }
}

// 处理注册表单提交
if ($_POST && isset($_POST['register'])) {
    $username = $_POST['username'] ?? '';
    $nickname = $_POST['nickname'] ?? '';
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    
    if (empty($username) || empty($nickname) || empty($password)) {
        $error = '用户名、昵称和密码都是必填的';
    } elseif ($password !== $confirm_password) {
        $error = '两次输入的密码不一致';
    } else {
        // 检查用户是否已存在
        $user_dir = __DIR__ . '/users/' . $username;
        $profile_file = $user_dir . '/profile.json';
        
        if (file_exists($profile_file)) {
            $error = '用户名已存在';
        } else {
            // 创建用户目录
            if (!file_exists($user_dir)) {
                mkdir($user_dir, 0755, true);
            }
            
            // 创建用户资料
            $profile_data = [
                'username' => $username,
                'nickname' => $nickname,
                'password' => password_hash($password, PASSWORD_DEFAULT),
                'created_at' => date('Y-m-d H:i:s')
            ];
            
            // 保存用户资料
            file_put_contents($profile_file, json_encode($profile_data, JSON_PRETTY_PRINT));
            
            $_SESSION['user_logged_in'] = true;
            $_SESSION['username'] = $username;
            $_SESSION['user_nickname'] = $nickname;
            header('Location: index.php');
            exit;
        }
    }
}
if ($_POST && isset($_POST['admin'])) {
     header('Location: /admin/index.php');
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lanxAI - login</title>
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
    <div class="login-container">
        <h1>lanxAI - 首个览星自研发AI</h1>
        <div class="subtitle"><?php echo $is_login ? '登录您的账户' : ($is_register ? '创建新账户' : '请登录或注册以继续'); ?></div>
        
        <?php if ($error): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <?php if (!$is_login && !$is_register): ?>
            <div style="text-align: center; margin-bottom: 20px;">
                <a href="?action=login" class="btn" style="margin-bottom: 10px; display: inline-block;">登录</a>
                <a href="/admin/" class="btn" style="margin-bottom: 10px; display: inline-block;">管理员登录</a>
                <a href="?action=register" class="btn" style="margin-bottom: 10px; display: inline-block;">注册</a>
            </div>
        <?php else: ?>
            <form method="post">
                <?php if ($is_register): ?>
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="nickname">昵称</label>
                        <input type="text" id="nickname" name="nickname" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" name="password" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="confirm_password">确认密码</label>
                        <input type="password" id="confirm_password" name="confirm_password" required>
                    </div>
                    
                    <input type="hidden" name="register" value="1">
                    <button type="submit" class="btn">注册</button>
                    
                    <div class="existing-user">
                        <p>已有账户？<a href="?action=login">点击登录</a></p>
                    </div>
                <?php else: ?>
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" name="password" required>
                    </div>
                    
                    <input type="hidden" name="login" value="1">
                    <button type="submit" class="btn">登录</button>
                    
                    <div class="existing-user">
                        <p>没有账户？<a href="?action=register">点击注册</a></p>
                    </div>
                <?php endif; ?>
            </form>
        <?php endif; ?>
        
        <div class="footer">
            LANX开源AI
        </div>
    </div>
</body>
</html>
